package com.qhhc.service.impl;

import com.qhhc.entity.CustomerServiceMessage;
import com.qhhc.mapper.CustomerServiceMessageMapper;
import com.qhhc.service.CustomerServiceMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 客服消息服务实现类
 */
@Service
public class CustomerServiceMessageServiceImpl implements CustomerServiceMessageService {

    @Autowired
    private CustomerServiceMessageMapper customerServiceMessageMapper;

    /**
     * 发送消息
     *
     * @param message 消息信息
     * @return 结果
     */
    @Override
    public int sendMessage(CustomerServiceMessage message) {
        if (message.getCreateTime() == null) {
            message.setCreateTime(new Date());
        }
        message.setUpdateTime(new Date());
        return customerServiceMessageMapper.insertMessage(message);
    }

    /**
     * 获取会话消息列表
     *
     * @param sessionId 会话ID
     * @return 消息列表
     */
    @Override
    public List<CustomerServiceMessage> getMessagesBySessionId(String sessionId) {
        return customerServiceMessageMapper.selectMessagesBySessionId(sessionId);
    }

    /**
     * 更新消息状态
     *
     * @param id     消息ID
     * @param status 状态
     * @return 结果
     */
    @Override
    public int updateMessageStatus(Long id, Integer status) {
        return customerServiceMessageMapper.updateMessageStatus(id, status);
    }

    /**
     * 批量更新会话中的消息状态
     *
     * @param sessionId 会话ID
     * @param status    状态
     * @return 结果
     */
    @Override
    public int updateMessagesStatusBySessionId(String sessionId, Integer status) {
        return customerServiceMessageMapper.updateMessagesStatusBySessionId(sessionId, status);
    }

    /**
     * 获取未读消息数量
     *
     * @param sessionId  会话ID
     * @param senderType 发送者类型
     * @return 未读消息数量
     */
    @Override
    public int getUnreadMessageCount(String sessionId, Integer senderType) {
        return customerServiceMessageMapper.countUnreadMessages(sessionId, senderType);
    }

    /**
     * 分页查询消息
     *
     * @param params 查询参数
     * @return 消息列表
     */
    @Override
    public List<CustomerServiceMessage> getMessageList(Map<String, Object> params) {
        return customerServiceMessageMapper.selectMessageList(params);
    }

    /**
     * 查询消息数量
     *
     * @param params 查询参数
     * @return 消息数量
     */
    @Override
    public int getMessageCount(Map<String, Object> params) {
        return customerServiceMessageMapper.selectMessageCount(params);
    }
} 